

//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/CallBack.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function CallBack(){}
CallBack.create = function( p_func, p_context, p_args )
{
	var func = function()
	{
		if( arguments != undefined )
		{
			//if( typeof( p_args ) == "Array" )
			//{
				//p_args.concat(arguments);
			//}
			//else
			//{
				p_args = arguments;
			//}
		}
		
		p_func.apply( p_context, p_args );
	};
	
	return func;
};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/Main.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

 
 
function Main()
{
	this.init();
}

Main.prototype.init = function()
{
	ApplicationFacade.getInstance().init();
	ApplicationFacade.getInstance().sendNotification( ApplicationNotification.START_APP ); 	
};



window.onload = function()
{
	POO.run();
	new Main();
};


window.requestAnimationFrame = (function () {

  return window.requestAnimationFrame || //Chromium 
  window.webkitRequestAnimationFrame || //Webkit
  window.mozRequestAnimationFrame || //Mozilla Geko
  window.oRequestAnimationFrame || //Opera Presto
  window.msRequestAnimationFrame || //IE Trident?

  function (callback, element) { //Fallback function
    window.setTimeout(callback, 1);
  }
 
})();







//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/const/ApplicationNotification.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function ApplicationNotification(){}

	// static
	
	ApplicationNotification.START_APP 				= "startApp";
	ApplicationNotification.LOAD_DATA_COMPLETE	 	= "loadDataComplete";
	ApplicationNotification.LOAD_DATA_PROGRESS	 	= "loadDataProgress";
	ApplicationNotification.ADD_ANIMATION	 		= "addAnimation";
	ApplicationNotification.CLEAN_MAP	 			= "cleanMap";
	ApplicationNotification.LOAD_MAP	 			= "loadMap";
	ApplicationNotification.LOAD_PROJECT	 		= "loadProject";
	ApplicationNotification.UNLOAD_PROJECT	 		= "unloadProject";
	ApplicationNotification.LOAD_PROJECT_COMPLETE	= "loadProjectComplete";



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/controller/AddAnimationCommand.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function AddAnimationCommand(){}; 
POO.extend( AddAnimationCommand, Command );



AddAnimationCommand.prototype.execute = function( p_notificationType, p_data )
{
	
	var facade = ApplicationFacade.getInstance(); 
	
	var appProxy = facade.getProxy( ApplicationProxy.NAME );
	var mapProxy = facade.getProxy( MapProxy.NAME );
	var animProxy = facade.getProxy( AnimationProxy.NAME );
	
	var currentMap = appProxy.currentMap;
	var currentLayer = appProxy.currentLayer;
	
	var mapVO = mapProxy.getMapVOById( currentMap );
	
	if( mapVO == null )
		return;
	
	var layerVO = mapVO.getLayerVOById( currentLayer );
	
	if( layerVO == null )
		return;
	
	var animVO = animProxy.getAnimationVOById(p_data.animId);
	
	if( animVO == null )
		return;
	
	//p_data.layerId
	//x,y,z,width,height,alpha,animated:true||false,texId||animId,movable:true||false
	layerVO.displayList.push(
	
				[p_data.x, p_data.y, 0, animVO.width, animVO.height, 1, true, p_data.animId, true ]
				
	);
	
};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/controller/LoadMapCommand.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function LoadMapCommand(){}; 
POO.extend( LoadMapCommand, Command );



LoadMapCommand.prototype.execute = function( p_notificationType, p_data )
{
	var facade = ApplicationFacade.getInstance(); 
	var appProxy = facade.getProxy( ApplicationProxy.NAME );
	facade.sendNotification( ApplicationNotification.CLEAN_MAP );
	appProxy.currentMap = p_data;
};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/controller/LoadProjectCommand.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function LoadProjectCommand(){}; 
POO.extend( LoadProjectCommand, Command );



LoadProjectCommand.prototype.execute = function( p_notificationType, p_data )
{
	var facade = ApplicationFacade.getInstance(); 
	var appProxy = facade.getProxy( ApplicationProxy.NAME );
	// unload previous project
	facade.sendNotification( ApplicationNotification.UNLOAD_PROJECT );
	appProxy.loadProject( p_data );
};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/controller/StartApplicationCommand.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function StartApplicationCommand(){}; 
POO.extend( StartApplicationCommand, Command );



StartApplicationCommand.prototype.execute = function( p_notificationType, p_data )
{
	var mapmediator 	= new MapMediator(document.getElementById("uiContainer"));
	var appproxy 		= new ApplicationProxy();
	var texproxy		= new TextureProxy();
	var animProxy		= new AnimationProxy();
	var mapProxy		= new MapProxy();
	var geProxy			= new GameEventProxy();
	
	ApplicationFacade.getInstance().registerMediator( MapMediator.NAME, mapmediator );
	ApplicationFacade.getInstance().registerProxy( AnimationProxy.NAME, animProxy );
	ApplicationFacade.getInstance().registerProxy( TextureProxy.NAME, texproxy );
	ApplicationFacade.getInstance().registerProxy( ApplicationProxy.NAME, appproxy );
	ApplicationFacade.getInstance().registerProxy( MapProxy.NAME, mapProxy );
	
	
	ApplicationFacade.getInstance().sendNotification( ApplicationNotification.LOAD_PROJECT, './assets/projects/google.js' );
	
};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/facade/ApplicationFacade.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

	
	
function ApplicationFacade(){}
POO.extend( ApplicationFacade, Facade );


	// static
	
	ApplicationFacade._instance = null;

	ApplicationFacade.getInstance = function()
	{
		ApplicationFacade._instance = ApplicationFacade._instance || new ApplicationFacade();
		return ApplicationFacade._instance;
	};
	
	
	// public
	
	ApplicationFacade.prototype.init = function()
	{
		this._super.init.apply( this );
		
		this.registerCommand( ApplicationNotification.START_APP, StartApplicationCommand );
		this.registerCommand( ApplicationNotification.ADD_ANIMATION, AddAnimationCommand );
		this.registerCommand( ApplicationNotification.LOAD_MAP, LoadMapCommand );
		this.registerCommand( ApplicationNotification.LOAD_PROJECT, LoadProjectCommand );
	};
	
	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/AnimationProxy.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function AnimationProxy()
{
	this.init( AnimationProxy.NAME, new Array() );
}

POO.extend( AnimationProxy, Proxy );


	// static
	
	AnimationProxy.NAME = "AnimationProxy";
	
	//public
	
	AnimationProxy.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [ p_name, p_data ] );
		this.listenNotifications();
	};
	
	AnimationProxy.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.LOAD_PROJECT_COMPLETE, this.onNotification );
	};

	AnimationProxy.prototype.onNotification = function( p_notificationType, p_data )
	{
		switch ( p_notificationType )
		{
			case ApplicationNotification.LOAD_PROJECT_COMPLETE:	
				this._parseAnimationData(); 
				break;
		}
	};
	
	AnimationProxy.prototype.getAnimationVOById = function( p_id )
	{
		var vo = null;
		var i = 0;
		var max = this._data.length;
		
		for( ; i < max; i++ )
		{
			vo = this._data[i];
			if( vo.name == p_id )
				return vo;
		}
		
		return null;
	};

	
	// private
	AnimationProxy.prototype._parseAnimationData = function()
	{
		var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var data = proxy.getAnimationsList();
		var i = 0;
		var max = data.length;
		var vo = null;
		var obj = null;
		
		for( ; i < max; i++ )
		{
			obj = data[i];
			vo = new AnimationVO();
			vo.name = obj.name;
			vo.width = obj.width;
			vo.height = obj.height;
			vo.animTime = obj.animTime;
			vo.frames = obj.frames;
			
			this._data.push( vo );
		}
	};
	

	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/ApplicationProxy.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function ApplicationProxy()
{
	this.init( ApplicationProxy.NAME, new Array() );
}

POO.extend( ApplicationProxy, Proxy );


	// static
	ApplicationProxy.NAME = "ApplicationProxy";


	//public
	ApplicationProxy.prototype.currentAnim 		= "";
	ApplicationProxy.prototype.currentLayer 	= "";
	ApplicationProxy.prototype.currentMap 		= "";
	ApplicationProxy.prototype._currentProject  = null;
	ApplicationProxy.prototype._loader			= null;
	
	
	ApplicationProxy.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [ p_name, p_data ] );
		this.listenNotifications();
		this._loader = new Loader();
		this._loader.signal.addListener( Loader.LOAD_COMPLETE, this._loadProjectCompleteHandler, this );
	};
	
	ApplicationProxy.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.SELECT_ANIMATION, this.onNotification );
		this.addNotificationListener( ApplicationNotification.SELECT_LAYER, 	this.onNotification );
		this.addNotificationListener( ApplicationNotification.SELECT_MAP, 		this.onNotification );
	};

	ApplicationProxy.prototype.onNotification = function( p_notificationType, p_data )
	{
		switch ( p_notificationType )
		{
			case ApplicationNotification.SELECT_ANIMATION:	this.currentAnim 	= p_data;	break;
			case ApplicationNotification.SELECT_LAYER:		this.currentLayer 	= p_data;	break;
			case ApplicationNotification.SELECT_MAP:		this.currentMap 	= p_data;	break;
		}
	};
	
	
	ApplicationProxy.prototype.loadProject = function( p_url )
	{
		this._loader.load( p_url );
	};
	
	ApplicationProxy.prototype._loadProjectCompleteHandler = function()
	{
		try
		{
			this._currentProject = eval( this._loader.getData() );
		}
		catch(error)
		{
			this._currentProject = this.getDefaultProject();
		}
		
		this.getFacade().sendNotification( ApplicationNotification.LOAD_PROJECT_COMPLETE );
	};
	
	ApplicationProxy.prototype.getAnimationsList = function()
	{
		return this._currentProject.getAnimationsList();
	};
	
	ApplicationProxy.prototype.getTexturesList = function()
	{
		return this._currentProject.getTexturesList();
	};
	
	ApplicationProxy.prototype.getMapsList = function()
	{
		return this._currentProject.getMapsList();
	};	
	
	ApplicationProxy.prototype.getGameEvents = function()
	{
		return this._currentProject.getGameEvents();
	};
	
	ApplicationProxy.prototype.getGlobalVars = function()
	{
		return this._currentProject.getGlobalVars();
	};
	
	ApplicationProxy.prototype.getProjectName = function()
	{
		return this._currentProject.name;
	};
	
	ApplicationProxy.prototype.getDefaultProject = function()
	{
		var project = new Object();
		project.name = "default";

		project.getAnimationsList = function(){return [];};
		project.getTexturesList = function(){return [];};
		project.getMapsList = function(){return [];};

		return project;
	};

	
	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/GameEventProxy.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function GameEventProxy()
{
	this.init( GameEventProxy.NAME, new Array() );
}

POO.extend( GameEventProxy, Proxy );


	// static
	
	GameEventProxy.NAME = "GameEventProxy";
	
	//public
	
	GameEventProxy.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [ p_name, p_data ] );
		this.listenNotifications();
	};
	
	GameEventProxy.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.LOAD_PROJECT_COMPLETE, this.onNotification );
		this.addNotificationListener( ApplicationNotification.LAUNCH_TRIGGER, this.onNotification );
	};

	GameEventProxy.prototype.onNotification = function( p_notificationType, p_data )
	{
		switch ( p_notificationType )
		{
			case ApplicationNotification.LAUNCH_TRIGGER:	
				this._launchTrigger(p_data); 
				break;
				
			case ApplicationNotification.LOAD_PROJECT_COMPLETE:	
				this._parseEvents(); 
				break;
		}
	};
	
	GameEventProxy.prototype._launchTrigger = function( p_trigger )
	{
		console.log("launch trigger");
	};

	
	// private
	GameEventProxy.prototype._parseEvents = function()
	{
		var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var data = proxy.getGameEvents();
		var i = 0;
		var max = data.length;
		var obj = null;
		
		this._data = new Array();
		
		for( ; i < max; i++ )
		{
			this._data.push( data[i] );
		}
	};
	

	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/MapProxy.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function MapProxy()
{
	this.init( MapProxy.NAME, new Array() );
}

POO.extend( MapProxy, Proxy );


	// static
	
	MapProxy.NAME = "MapProxy";
	
	//public
	
	MapProxy.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [ p_name, p_data ] );
		this.listenNotifications();
	};
	
	MapProxy.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.LOAD_PROJECT_COMPLETE, this.onNotification );
	};

	MapProxy.prototype.onNotification = function( p_notificationType, p_data )
	{
		switch ( p_notificationType )
		{
			case ApplicationNotification.LOAD_PROJECT_COMPLETE:	
				this._parseMapData(); 
				break;
		}
	};
	
	MapProxy.prototype.getMapVOById = function( p_id )
	{
		var vo = null;
		var i = 0;
		var max = this._data.length;
		
		for( ; i < max; i++ )
		{
			vo = this._data[i];
			if( vo.name == p_id )
				return vo;
		}
		
		return null;
	};
	
	MapProxy.prototype.removeMapVOById = function( p_id )
	{
		var vo = this.getMapVOById(p_id);
		var index = -1;
		if( vo != null )
		{
			index = this._data.indexOf(vo);
			this._data.splice( index, 1 );
		}
	};
	
	MapProxy.prototype.addMapVO = function( p_vo )
	{
		this._data.push( p_vo );
	};

	
	// private
	MapProxy.prototype._parseMapData = function()
	{
		var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var data = proxy.getMapsList();
		var i = 0;
		var j = 0;
		var max = data.length;
		var ma2 = data.length;
		var vo = null;
		var layerVO = null;
		var obj = null;
		var obj2 = null;
		
		for( ; i < max; i++ )
		{
			obj = data[i];
			vo = new MapVO();
			
			vo.layers = new Array();
			max2 = obj.layers.length;
			
			for( j = 0; j < max2; j++ )
			{
				obj2 = obj.layers[j];
				layerVO = new LayerVO();
				layerVO.name = obj2.name;
				layerVO.lazy = obj2.lazy;
				layerVO.sortFunc = obj2.sortFunc;
				layerVO.sortArgs = obj2.sortArgs;
				layerVO.displayList = obj2.displayList;
				
				vo.layers.push( layerVO );
			}
			
			vo.name = obj.name;
			vo.type = obj.type;
			this._data.push( vo );
		}
		
	};
	

	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/TextureProxy.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function TextureProxy()
{
	this.init( TextureProxy.NAME, new Array() );
}

POO.extend( TextureProxy, Proxy );


	// static
	
	TextureProxy.NAME = "TextureProxy";

	
	//private
	TextureProxy.prototype._loader 	= null;
	TextureProxy.prototype._list 	= null;
	
	//public
	
	TextureProxy.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [ p_name, p_data ] );
		this._loader = new ImageLoader();
		this._loader.signal.addListener( ImageLoader.LOAD_PROGRESS, this._loadProgressHandler, this );
		this._loader.signal.addListener( ImageLoader.LOAD_COMPLETE, this._loadCompleteHandler, this );
		this.listenNotifications();
	};
	
	TextureProxy.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.LOAD_PROJECT_COMPLETE, this.onNotification );
	};

	TextureProxy.prototype.onNotification = function( p_notificationType, p_data )
	{
		switch ( p_notificationType )
		{
			case ApplicationNotification.LOAD_PROJECT_COMPLETE:	
				this._loadTextures(); 
				break;
		}
	};
	
	
	// private
	TextureProxy.prototype._loadTextures = function( p_from, p_to )
	{
		this._list = new Array();
		this._loader.clean();
		var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var data = proxy.getTexturesList();
		var i = 0;
		var max = data.length;
		
		for( ; i < max; i++ )
		{
			this._loader.addFile(data[i], i);
			this._list.push( data[i] );
		}
		
		this._loader.load();
	};
	
	TextureProxy.prototype._loadProgressHandler = function()
	{
		var progression = this._loader.getProgression();
		this.getFacade().sendNotification(ApplicationNotification.LOAD_DATA_PROGRESS, {"progression":progression});
	};
	
	TextureProxy.prototype._loadCompleteHandler = function()
	{
		var appProxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var manager = TextureManager.getInstance();
		var data = this._loader.getData();
		var texture = null;
		var id = null;
		
		for( id in data )
		{
			texture = SmartTexture.createTexture();
			texture.id = parseInt(id);
			texture.data = data[id];
			texture.type = Texture2D.BMP_TEXTURE;
			manager.addTexture( texture );
		}
		
		this.getFacade().sendNotification(ApplicationNotification.LOAD_DATA_COMPLETE);
	};
	
	TextureProxy.prototype.getTexturesList = function()
	{
		return this._list;
	};

	
	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/texture/SmartTexture.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function SmartTexture(){}; 
POO.extend( SmartTexture, Texture2D );


	// static
	SmartTexture._instances = new Array();

	SmartTexture.createTexture = function()
	{
		var texture = null;
		
		if( SmartTexture._instances.length > 0 )
		{
			texture = SmartTexture._instances.shift();
		}
		else
		{
			texture = new SmartTexture();
		}
		
		return texture;
	};
	
	
	//public
	SmartTexture.prototype.recycle = function()
	{
		this.data = null;
		this.url = null;
		this.id = -1;
		this.color = "#000000";
		this.type = Texture2D.COLOR_TEXTURE;
		
		SmartTexture._instances.push( this );
	};
	
	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/vo/AnimationVO.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function AnimationVO(){}


AnimationVO.prototype.name = null;
AnimationVO.prototype.width = 0;
AnimationVO.prototype.height = 0;
AnimationVO.prototype.animTime = 16;

AnimationVO.prototype.frames = null;

//{"uv":[0.25, 	0.25, 	0.5, 	0.5	],	"tex":2},
//{"uv":[0.5, 	0.5, 	0.75, 	0.75],	"tex":2},
//{"uv":[0.5, 	0.5, 	0.75, 	0.75],	"tex":2},
//{"uv":[0.75, 	0.75, 	1, 		1],		"tex":2},
//{"uv":[0, 		0, 		1, 		1],		"tex":2}



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/vo/GameEvent.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function GameEvent(){}

	GameEvent.prototype.trigger = null;
	GameEvent.prototype.exec = function(){};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/vo/LayerVO.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function LayerVO()
{
	this.displayList = new Array();
}

LayerVO.prototype.lazy 			= false;
LayerVO.prototype.sortFunc 		= null;
LayerVO.prototype.sortArgs 		= null;
LayerVO.prototype.name 			= "";
LayerVO.prototype.displayList 	= null;



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/model/vo/MapVO.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function MapVO()
{
	this.layers = new Array();
}


MapVO.prototype.name = "";
MapVO.prototype.type = "";
MapVO.prototype.layers = null;

MapVO.prototype.removeLayerVOById = function( p_id )
{
	var i = 0;
	var max = this.layers.length;
	var vo = null;
	var layers = new Array();
	
	for( i = 0; i < max; i++ )
	{
		vo = this.layers[i];
		if( vo.name != p_id )
		{
			layers.push(vo);
		}
	}
	
	this.layers = layers;
};

MapVO.prototype.getLayerVOById = function( p_id )
{
	var i = 0;
	var max = this.layers.length;
	var vo = null;
	
	for( i = 0; i < max; i++ )
	{
		vo = this.layers[i];
		if( vo.name == p_id )
		{
			return vo;
		}
	}
	
	return null;
};


//{
	//"name":"home",
	//"type":"isomap",
	//"layers":[
		//{
			//"lazy":false,
			//"sortFunc":null,
			//"sortArgs":null,
			//"name":"ground",
			//"displayList":[
				//[64,32,0,64,32,1,true,"grass",false],
				//[96,48,0,64,32,1,true,"grass",false]
			//]
		//},
		//{
			//"lazy":false,
			//"sortFunc":null,
			//"sortArgs":null,
			//"name":"ground",
			//"displayList":[
				//[64,32,0,64,32,1,true,"frog",false],
				//[96,48,0,64,32,1,true,"moulin",false]
			//]
		//},
	//]
//}	



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/utils/Loader.js**************************

//***********************************************************************

function Loader()
{
	this.signal = new Signal()
	this._xhr = new XMLHttpRequest();
}

	Loader.LOAD_COMPLETE = "loadComplete";
	
	Loader.prototype.signal = null;
	Loader.prototype._xhr	= null;
	Loader.prototype._data	= null;
	
	Loader.prototype.load = function( p_url, p_async )
	{
		var _this = this;
		p_async = p_async || false;
		
		this._xhr.onreadystatechange = function()
		{
			_this.completeHandler();
		};
		
		this._xhr.open( "GET", p_url, p_async );
		this._xhr.send(null);
	};
	
	
	Loader.prototype.completeHandler = function()
	{
		if( this._xhr.readyState == 4 )
		{
			this._data = this._xhr.responseText;
			this.signal.dispatchEvent( Loader.LOAD_COMPLETE );
		}
	};
	
	Loader.prototype.getData = function()
	{
		return this._data;
	};




//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/view/MapMediator.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function MapMediator( p_view )
{
	this.init( MapMediator.NAME, p_view );
}

POO.extend( MapMediator, Mediator );


	// static
	MapMediator.NAME = "MapMediator";

	//private
	MapMediator.prototype._screen = null;
	
	
	// public
	MapMediator.prototype.init = function( p_name, p_data )
	{
		this._super.init.apply( this, [p_name, p_data] );
		this.initRenderingContext();
		this.listenNotifications();	
	};
	
	MapMediator.prototype.initRenderingContext = function()
	{
		this._screen = new MapScreen();
		var container = this.getViewComponent();
		this._screen.init( container, 800, 600 );
	};
	
	MapMediator.prototype.listenNotifications = function()
	{
		this.addNotificationListener( ApplicationNotification.LOAD_DATA_COMPLETE, 	this.onNotification );
		this.addNotificationListener( ApplicationNotification.LOAD_DATA_PROGRESS, 	this.onNotification );
		this.addNotificationListener( ApplicationNotification.ADD_ANIMATION, 		this.onNotification );
		this.addNotificationListener( ApplicationNotification.CLEAN_MAP, 			this.onNotification );
		this.addNotificationListener( ApplicationNotification.LOAD_MAP, 			this.onNotification );
		
		//this._screen.signal.addListener( MapScreen.ADD_ANIM, this.addAnimHandler, 	this );
	};
	
	//MapMediator.prototype.addAnimHandler = function(p_data)
	//{
		//var proxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		//var animId = proxy.currentAnim;
		//var layerId = proxy.currentLayer;
		//
		//if( animId == "" )
			//return;
		//
		//this.getFacade().sendNotification( ApplicationNotification.ADD_ANIMATION, {"layerId":layerId,"animId":animId,"x":p_data.x,"y":p_data.y} );
	//};

	MapMediator.prototype.onNotification = function( p_notificationType, p_data )
	{
		var animProxy = this.getFacade().getProxy( AnimationProxy.NAME );
		var appProxy = this.getFacade().getProxy( ApplicationProxy.NAME );
		var mapProxy = this.getFacade().getProxy( MapProxy.NAME );
		
		switch ( p_notificationType )
		{
			case ApplicationNotification.LOAD_DATA_COMPLETE:
				this.getFacade().sendNotification( ApplicationNotification.LOAD_MAP, "foo" );
				break;
				
			case ApplicationNotification.LOAD_DATA_PROGRESS:
				this._screen.showProgression( p_data.progression );
				break;
				
			case ApplicationNotification.ADD_ANIMATION:
				var animVO = animProxy.getAnimationVOById(p_data.animId);
				this._screen.addSpriteAt( animVO, appProxy.currentLayer, p_data.x, p_data.y );
				break;
				
			case ApplicationNotification.CLEAN_MAP:
				this._screen.clean();
				break;
				
			case ApplicationNotification.LOAD_MAP:
				
				var vo = mapProxy.getMapVOById(appProxy.currentMap);
				var layerVO = null;
				var i = 0;
				var j = 0;
				var max = vo.layers.length;
				var max2 = 0;
				var animVO = null;
				var spriteData = null;
				
				for( i = 0; i < max ; i++ )
				{
					layerVO = vo.layers[i];
					this._screen.addLayer( layerVO.name );
					
					max2 = layerVO.displayList.length;
					
					for( j = 0; j < max2; j++ )
					{
						spriteData = layerVO.displayList[j];
						animVO = animProxy.getAnimationVOById(spriteData[7]);
						this._screen.addSpriteAt( animVO, layerVO.name, spriteData[0], spriteData[1] );
					}
				}
				
				break;
		}
	};




//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/view/component/IsoMap.js**************************

//***********************************************************************

function IsoMap()
{
	this._isoData = new Object();
}

POO.extend( IsoMap, Map );


	IsoMap.prototype._isoData = null;
	IsoMap.prototype.cellW = 64;
	IsoMap.prototype.cellH = 32;

	IsoMap.prototype.addSpriteAt = function( p_spr, p_layerId, p_x, p_y )
	{
		var layer = this.getLayer( p_layerId );
		
		if( layer == null )
		{
			return;
		}
		
		var isoCoords = IsoUtils.screenToIso( p_x, p_y );
		var row = isoCoords.row;
		var col = isoCoords.col;
		var remanagedCoords = IsoUtils.isoToScreen( row, col, this.cellW, this.cellH );
		var old = this._getIsoSpriteAt( p_layerId, row, col );
		
		if( old != null )
		{
			this.removeSprite(old);
		}
		
		p_spr.x = remanagedCoords.x;
		p_spr.y = remanagedCoords.y;
		
		this._pushIsoSpriteAt( p_spr, p_layerId, row, col );
		
		layer.displayList.addChild( p_spr );
		p_spr.transformGroup = this.camera.transformGroup;
	};
	
	IsoMap.prototype._pushIsoSpriteAt = function( p_spr, p_layerId, p_row, p_col )
	{
		if( !this._isoData[layerId] )
			this._isoData[layerId] = new Array();
		
		if( this._isoData[layerId][p_row] == undefined )
			this._isoData[layerId][p_row] = new Array();
			
		this._isoData[layerId][p_row][p_col] = p_spr;
	};
	
	IsoMap.prototype._getIsoSpriteAt = function( p_layerId, p_row, p_col )
	{
		if( !this._isoData[layerId] )
			this._isoData[layerId] = new Array();
		
		if( this._isoData[layerId][p_row] == undefined )
			this._isoData[layerId][p_row] = new Array();
			
		if( this._isoData[layerId][p_row][p_col] == undefined )
		{
			return null;
		}
		
		return this._isoData[layerId][p_row][p_col];
	};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/view/component/Map.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function Map()
{
}



	// private
	
	Map.prototype._renderer 		= null;
	Map.prototype._width			= 1;
	Map.prototype._height			= 1;
	Map.prototype.camera			= null;
	
	
	// public
	Map.prototype.init = function( p_camW, p_camH )
	{
		var camera = new Camera();		
		this.camera = camera;
		
		
		this._renderer = new Renderer2D();
		//render settings
		this._renderer.camera = camera;
		
		//camera
		camera.x = 0;
		camera.y = 0;
		camera.z = 0;
		camera.width = p_camW;
		camera.height = p_camH;
		camera.pivotX = p_camW * 0.5;
		camera.pivotY = p_camH * 0.5;
		camera.pivotZ = 0;
	};
	
	Map.prototype.addLayer = function( p_layerId )
	{
		var canvas = document.createElement("canvas");
		var layer = new Layer();
		
		canvas.width = this.camera.width;
		canvas.height = this.camera.height;
		
		layer.name = p_layerId;
		layer.displayList = new DisplayList();
		layer.canvas = canvas;
		layer.context = layer.canvas.getContext("2d");
		//layer1.lazy = true;
		
		//boost displayList
		
		var boost = new SimpleCulling();
		boost.init();
		layer.displayList.boost = boost;
		layer.sorted = false;
		
		this._renderer.layers.push( layer );
		
		return layer;
	};
	
	Map.prototype.removeLayer = function( p_layerId )
	{
		var layer = this.getLayer( p_layerId );
		
		if( layer == null )
		{
			return null;
		}
			
		var index = this._renderer.layers.indexOf( layer );
		this._renderer.layers.splice( index, 1 );
		return layer;
	};
	
	Map.prototype.getLayer = function( p_layerId )
	{
		var tab = this._renderer.layers;
		var max = tab.length;
		var i = 0;
		var layer = null;
		
		for( i = 0; i < max; i++ )
		{
			layer = tab[i];
			
			if( layer.name == p_layerId )
				return layer;
		}
		
		return null;
	};
	
	Map.prototype.removeSprite = function( p_spr )
	{
		if( p_spr.displayList )
		{
			p_spr.displayList.removeChild( p_spr );
			p_spr.displayList = null;
		}
	};
	
	Map.prototype.addSpriteAt = function( p_spr, p_layerId, p_x, p_y )
	{
		var layer = this.getLayer( p_layerId );
		
		if( layer == null )
		{
			return;
		}
		
		p_spr.x = p_x;
		p_spr.y = p_y;
		
		layer.displayList.addChild( p_spr );
		p_spr.transformGroup = this.camera.transformGroup;
	};
	
	Map.prototype.getSpriteAt = function( p_layerId, p_x, p_y )
	{
		var layer = this.getLayer( p_layerId );
		var spr = null;
		
		if( layer != null )
		{
			spr = layer.displayList.getVisibleChildrenIn( p_x, p_y, 1, 1 )[0];
		}
		
		return spr;
	};
	
	Map.prototype.getLayers = function()
	{
		return this._renderer.layers;
	};

	
	// rendering loop
	Map.prototype.render = function()
	{
		this._renderer.render();
	};
		
	Map.prototype.clean = function()
	{
		this._renderer.layers = new Array();
	};
	
	




//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/view/factory/AnimationFactory.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function AnimationFactory(){}

AnimationFactory.getAnimationFromVO = function( p_vo )
{
	var anim = new Animation();
	anim.animationTime  = p_vo.animTime
	anim.name = p_vo.name;
	anim.frame = 0;
	
	var frames = p_vo.frames;
	var i = 0;
	var max = frames.length;
	
	for( ; i < max; i++ )
	{
		anim.setFrame( i, frames[i].uv, frames[i].tex );
	}
	
	return anim;
};



//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/view/screen/MapScreen.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function MapScreen()
{
	this.signal = new Signal();
}



	// private
	MapScreen.prototype._map				= null;
	MapScreen.prototype._mouseTracker 		= null;
	MapScreen.prototype._container			= null;
	MapScreen.prototype.signal				= null;
	
	// static
	MapScreen.ADD_ANIM					= "addAnim";
	
	
	
	// handlers
	MapScreen.prototype._dblClickHandler = function()
	{
		var x = this._mouseTracker.localX + this._map.camera.x;
		var y = this._mouseTracker.localY + this._map.camera.y;
		//this.signal.dispatchEvent( MapScreen.ADD_ANIM, [{"x":x,"y":y}] );
	};
	
	MapScreen.prototype._upHandler = function()
	{
		
	};
	
	MapScreen.prototype._downHandler = function()
	{
		
	};
	
	MapScreen.prototype._setEventListeners = function()
	{		
		 //initialize the mouse/touch tracker
		this._mouseTracker = new MouseTracker(this._container);
		
		this._mouseTracker.signal.addListener( MouseTracker.MOUSE_UP, this._upHandler, this );
		this._mouseTracker.signal.addListener( MouseTracker.MOUSE_DOWN, this._downHandler, this );
		this._mouseTracker.signal.addListener( MouseTracker.DOUBLE_CLICK, this._dblClickHandler, this );
	};
	
	
	
	// public
	MapScreen.prototype.init = function( p_container, p_camW, p_camH )
	{
		this._map = new Map();
		this._container = p_container;
		this._map.init( p_camW, p_camH );
		this._setEventListeners();
		this.resume();
	};
	
	
	MapScreen.prototype.zoomAt = function( p_x, p_y, p_zoom )
	{
		var zoom 	= p_zoom;
		var pivotX 	= p_x;
		var pivotY 	= p_y;
		
		this._map.camera.pivotX = pivotX;
		this._map.camera.pivotY = pivotY;
		//this._map.camera.zoom 	= zoom;
		
		TweenNano.to( this._map.camera, 1000, {"zoom": zoom} );
	};
	
	
	MapScreen.prototype.addSpriteAt = function( p_animVO, p_layerId, p_x, p_y )
	{
		var anim = AnimationFactory.getAnimationFromVO( p_animVO );
		var spr = new Sprite();
		
		spr.width 		= p_animVO.width;
		spr.height 		= p_animVO.height;
		spr.uv 			= anim.data[0].uv;
		spr.textureId 	= anim.data[0].tex;
		
		anim.sprite = spr;
		
		if( anim.data.length > 0 )
		{
			var rand = ( Math.random() * 1000000 ) >> 0;
			AnimationManager.getInstance().registerAnimation( anim, anim.name+"_"+rand );
		}
		
		this._map.addSpriteAt( spr, p_layerId, p_x, p_y );
	};
	
	
	MapScreen.prototype.addLayer = function( p_layerId )
	{
		var layer = this._map.addLayer( p_layerId );
		this._container.appendChild( layer.canvas );
	};
	
	
	MapScreen.prototype.removeLayer = function( p_layerId )
	{
		var layer = this._map.removeLayer( p_layerId );
		if( layer == null )
		{
			return;
		}
			
		this._container.removeChild( layer.canvas );
	};
	
	
	
	// rendering loop
	MapScreen.prototype.render = function()
	{
		AnimationManager.getInstance().animateAll();
		
		if( this._mouseTracker.down )
		{			
			this._map.camera.x -= ( this._mouseTracker.mouseX - this._mouseTracker.lastX );
			this._map.camera.y -= ( this._mouseTracker.mouseY - this._mouseTracker.lastY );
			this._mouseTracker.lastX = this._mouseTracker.mouseX;
			this._mouseTracker.lastY = this._mouseTracker.mouseY;
		}
		
		var time = new Date().getTime();
		this._map.render();
		time = new Date().getTime() - time;
	};
	
	
	MapScreen.prototype.resume = function()
	{
		FrameManager.getInstance().removeFrameListener( this.render, this );
		FrameManager.getInstance().addFrameListener( this.render, this );
	};
	
	
	MapScreen.prototype.pause = function()
	{
		FrameManager.getInstance().removeFrameListener( this.render, this );
	};
	
	
	MapScreen.prototype.clean = function()
	{
		var layers = this._map.getLayers();
		var layer = null;
		var i = 0;
		var max = layers.length;
		
		for( i = 0; i < max; i++ )
		{
			layer = layers[i];
			this._container.removeChild( layer.canvas );
		}
		
		this._map.clean();
		
		AnimationManager.getInstance().destroy();
	};
	

	

	// loading...
	MapScreen.prototype.showProgression = function( p_progression )
	{
		console.log(p_progression+"%");
		//var context = this._layer.context;
		//context.fillStyle = "white";
		//context.fillRect( 0, 100, 100, 20 );
		//context.fillStyle = "#666666";
		//context.font = '15px sans-serif';
		//context.fillText(p_progression+"%",10,100);
	};
	




//***********************************************************************

//**************************C:\wamp\www\game/src2/js/game/view/utils/IsoUtils.js**************************

//***********************************************************************

/**
 * ...
 * @author Thot
 */

function IsoUtils(){}
 
IsoUtils.prototype.screenToIso = function( p_x, p_y, p_cellW, p_cellH )
{
	//var l_coords:Point = new Point( 0, 0);
	
	//p_x -= p_nodeWidth * 0.5;
	
	/**
	 * 
	 * 	p_x = ( col - line ) * ( nodeWidth / 2 );
	 *	p_y = ( col + line ) * ( nodeHeight / 2 );
	 * 
	 *  ( col - line ) = p_x / ( nodeWidth / 2 );
	 *  ( col + line ) = p_y / ( nodeHeight / 2 );
	 * 
	 *   -2 * line = ( col - line ) - ( col + line ) = ( p_x / ( nodeWidth / 2 ) ) - ( p_y / ( nodeHeight / 2 ) );
	 *   line = ( ( p_x / ( nodeWidth / 2 ) ) - ( p_y / ( nodeHeight / 2 ) ) ) / -2;
	 *   
	 * 
	 *   2 * col = col - line + col + line = ( p_x / ( nodeWidth / 2 ) ) + ( p_y / ( nodeHeight / 2 ) );
	 *   col = ( ( p_x / ( nodeWidth / 2 ) ) + ( p_y / ( nodeHeight / 2 ) ) ) / 2
	 * 
	 * 
	 * 
	 */
	
	 var p_nodeHeight = p_cellW;
	 var p_nodeWidth = p_cellH;
	 
	var l_midHeight = p_nodeHeight >> 1;
	var l_midWidth = p_nodeWidth >> 1;
	
	p_x -= l_midWidth;
	
	var l_col = ( ( ( p_x / l_midWidth ) + ( p_y / l_midHeight ) ) * 0.5 ) << 0;
	var l_row = ( ( ( p_x / l_midWidth ) - ( p_y / l_midHeight ) ) * -0.5 ) << 0;
	
	return {"col":l_col, "row":l_row };
};

IsoUtils.prototype.isoToScreen = function(  p_row, p_col, p_cellW, p_cellH )
{
	var x = ( p_col - p_row ) * ( p_cellW * 0.5 );
	var y = ( p_col + p_row ) * ( p_cellH * 0.5 );
	
	return {"x":x, "y":y };
};



